package cn.edu.scau.cmi.hombio.web.rest;

import cn.edu.scau.cmi.hombio.dao.MedicineStorageDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineSupplierDAO;
import cn.edu.scau.cmi.hombio.dao.SupplierDAO;

import cn.edu.scau.cmi.hombio.domain.MedicineStorage;
import cn.edu.scau.cmi.hombio.domain.MedicineSupplier;
import cn.edu.scau.cmi.hombio.domain.Supplier;

import cn.edu.scau.cmi.hombio.service.SupplierService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for Supplier entities
 * 
 */

@Controller("SupplierRestController")

public class SupplierRestController {

	/**
	 * DAO injected by Spring that manages MedicineStorage entities
	 * 
	 */
	@Autowired
	private MedicineStorageDAO medicineStorageDAO;

	/**
	 * DAO injected by Spring that manages MedicineSupplier entities
	 * 
	 */
	@Autowired
	private MedicineSupplierDAO medicineSupplierDAO;

	/**
	 * DAO injected by Spring that manages Supplier entities
	 * 
	 */
	@Autowired
	private SupplierDAO supplierDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Supplier entities
	 * 
	 */
	@Autowired
	private SupplierService supplierService;

	/**
	 * Show all MedicineSupplier entities by Supplier
	 * 
	 */
	@RequestMapping(value = "/Supplier/{supplier_id}/medicineSuppliers", method = RequestMethod.GET)
	@ResponseBody
	public List<MedicineSupplier> getSupplierMedicineSuppliers(@PathVariable Integer supplier_id) {
		return new java.util.ArrayList<MedicineSupplier>(supplierDAO.findSupplierByPrimaryKey(supplier_id).getMedicineSuppliers());
	}

	/**
	* Save an existing Supplier entity
	* 
	*/
	@RequestMapping(value = "/Supplier", method = RequestMethod.PUT)
	@ResponseBody
	public Supplier saveSupplier(@RequestBody Supplier supplier) {
		supplierService.saveSupplier(supplier);
		return supplierDAO.findSupplierByPrimaryKey(supplier.getId());
	}

	/**
	* Select an existing Supplier entity
	* 
	*/
	@RequestMapping(value = "/Supplier/{supplier_id}", method = RequestMethod.GET)
	@ResponseBody
	public Supplier loadSupplier(@PathVariable Integer supplier_id) {
		return supplierDAO.findSupplierByPrimaryKey(supplier_id);
	}

	/**
	* Delete an existing MedicineStorage entity
	* 
	*/
	@RequestMapping(value = "/Supplier/{supplier_id}/medicineStorages/{medicinestorage_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteSupplierMedicineStorages(@PathVariable Integer supplier_id, @PathVariable Integer related_medicinestorages_id) {
		supplierService.deleteSupplierMedicineStorages(supplier_id, related_medicinestorages_id);
	}

	/**
	* Delete an existing MedicineSupplier entity
	* 
	*/
	@RequestMapping(value = "/Supplier/{supplier_id}/medicineSuppliers/{medicinesupplier_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteSupplierMedicineSuppliers(@PathVariable Integer supplier_id, @PathVariable Integer related_medicinesuppliers_id) {
		supplierService.deleteSupplierMedicineSuppliers(supplier_id, related_medicinesuppliers_id);
	}

	/**
	* Save an existing MedicineStorage entity
	* 
	*/
	@RequestMapping(value = "/Supplier/{supplier_id}/medicineStorages", method = RequestMethod.PUT)
	@ResponseBody
	public MedicineStorage saveSupplierMedicineStorages(@PathVariable Integer supplier_id, @RequestBody MedicineStorage medicinestorages) {
		supplierService.saveSupplierMedicineStorages(supplier_id, medicinestorages);
		return medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorages.getId());
	}

	/**
	* Save an existing MedicineSupplier entity
	* 
	*/
	@RequestMapping(value = "/Supplier/{supplier_id}/medicineSuppliers", method = RequestMethod.PUT)
	@ResponseBody
	public MedicineSupplier saveSupplierMedicineSuppliers(@PathVariable Integer supplier_id, @RequestBody MedicineSupplier medicinesuppliers) {
		supplierService.saveSupplierMedicineSuppliers(supplier_id, medicinesuppliers);
		return medicineSupplierDAO.findMedicineSupplierByPrimaryKey(medicinesuppliers.getId());
	}

	/**
	* Create a new Supplier entity
	* 
	*/
	@RequestMapping(value = "/Supplier", method = RequestMethod.POST)
	@ResponseBody
	public Supplier newSupplier(@RequestBody Supplier supplier) {
		supplierService.saveSupplier(supplier);
		return supplierDAO.findSupplierByPrimaryKey(supplier.getId());
	}

	/**
	* View an existing MedicineSupplier entity
	* 
	*/
	@RequestMapping(value = "/Supplier/{supplier_id}/medicineSuppliers/{medicinesupplier_id}", method = RequestMethod.GET)
	@ResponseBody
	public MedicineSupplier loadSupplierMedicineSuppliers(@PathVariable Integer supplier_id, @PathVariable Integer related_medicinesuppliers_id) {
		MedicineSupplier medicinesupplier = medicineSupplierDAO.findMedicineSupplierByPrimaryKey(related_medicinesuppliers_id, -1, -1);

		return medicinesupplier;
	}

	/**
	* Create a new MedicineSupplier entity
	* 
	*/
	@RequestMapping(value = "/Supplier/{supplier_id}/medicineSuppliers", method = RequestMethod.POST)
	@ResponseBody
	public MedicineSupplier newSupplierMedicineSuppliers(@PathVariable Integer supplier_id, @RequestBody MedicineSupplier medicinesupplier) {
		supplierService.saveSupplierMedicineSuppliers(supplier_id, medicinesupplier);
		return medicineSupplierDAO.findMedicineSupplierByPrimaryKey(medicinesupplier.getId());
	}

	/**
	* Register custom, context-specific property editors
	* 
	*/
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	* Delete an existing Supplier entity
	* 
	*/
	@RequestMapping(value = "/Supplier/{supplier_id}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteSupplier(@PathVariable Integer supplier_id) {
		Supplier supplier = supplierDAO.findSupplierByPrimaryKey(supplier_id);
		supplierService.deleteSupplier(supplier);
	}

	/**
	* Show all MedicineStorage entities by Supplier
	* 
	*/
	@RequestMapping(value = "/Supplier/{supplier_id}/medicineStorages", method = RequestMethod.GET)
	@ResponseBody
	public List<MedicineStorage> getSupplierMedicineStorages(@PathVariable Integer supplier_id) {
		return new java.util.ArrayList<MedicineStorage>(supplierDAO.findSupplierByPrimaryKey(supplier_id).getMedicineStorages());
	}

	/**
	* View an existing MedicineStorage entity
	* 
	*/
	@RequestMapping(value = "/Supplier/{supplier_id}/medicineStorages/{medicinestorage_id}", method = RequestMethod.GET)
	@ResponseBody
	public MedicineStorage loadSupplierMedicineStorages(@PathVariable Integer supplier_id, @PathVariable Integer related_medicinestorages_id) {
		MedicineStorage medicinestorage = medicineStorageDAO.findMedicineStorageByPrimaryKey(related_medicinestorages_id, -1, -1);

		return medicinestorage;
	}

	/**
	* Show all Supplier entities
	* 
	*/
	@RequestMapping(value = "/Supplier", method = RequestMethod.GET)
	@ResponseBody
	public List<Supplier> listSuppliers() {
		return new java.util.ArrayList<Supplier>(supplierService.loadSuppliers());
	}

	/**
	* Create a new MedicineStorage entity
	* 
	*/
	@RequestMapping(value = "/Supplier/{supplier_id}/medicineStorages", method = RequestMethod.POST)
	@ResponseBody
	public MedicineStorage newSupplierMedicineStorages(@PathVariable Integer supplier_id, @RequestBody MedicineStorage medicinestorage) {
		supplierService.saveSupplierMedicineStorages(supplier_id, medicinestorage);
		return medicineStorageDAO.findMedicineStorageByPrimaryKey(medicinestorage.getId());
	}
}